home *** CD-ROM | disk | FTP | other *** search
- #include <MacIncludes.h>
- #include "rk_button.h"
- #include "file+rk.h"
- #include "Rk.h"
- #include "Structs.h"
-
- extern PrefHandle gStartPrefs;
- extern char *p2cstr(Str255);
- extern char *PathNameFromDirID(long DirID, short vRefNum, char *s);
- extern char *pStrcat(Str255,Str255);
- extern Boolean gLowMem,gReport;
- Ptr gExtraMems;
-
-
- /** shared with rk_file/file+rk **/
- char first[MAX_SET], /* the first letter of each pred */
- context[MAX_CMD_LINE_LENGTH] = "\07", /* last chars entered */
- old_context[MAX_CMD_LINE_LENGTH] = "\07";
- Buffer Buf, CBuf; /* ptrs into the model k levels */
- char *prime_file; /* file to prime from and log to */
- int next_free = 0; /* index of next free node avail */
- long psize; /* # chars to prime from prime_file*/
- /* zero freqs, maybe overwritten */
- static char zero_freq[128] = {
- '\n', ' ', 'e', 's', 't', 'r', 'a', 'l',
- 'n', '.', 'c', 'i', 'm', 'o', 'd', 'h',
- 'p', 'u', 'f', 'b', 'w', 'g', '-', 'y',
- '/', 'v', 'k', '*', 'x', '5', '1', '2',
- '4', '>', 'q', '`', '3', 'z', '0', 'j',
- 'M', 'I', '6', '\'', 'E', '~', 'A', ',',
- 'S', 'D', 'R', '?', 'C', '|', 'B', 'T',
- 'P', 'U', '!', '_', '<', 'N', '8', '@',
- '&', '7', '\\', '"', 'J', ')', '(', '[',
- ']', 'F', 'L', ':', 'O', 'K', '9', 'H',
- '+', '$', 'W', 'Y', '=', 'G', ';', '^',
- '{', 'X', 'Q', '#', '}', 'V', '%', 'Z',
- /* '`', '\b', '\t', '\26', '\00', '\01', '\02', '\03',
- '\04', '\05', '\06', '\13', '\f', '\r', '\16', '\17',
- '\20', '\21', '\22', '\23', '\24', '\25', '\27', '\30',
- '\31', '\32', '\33', '\34', '\35', '\36', '\37','\177',*/};
-
- char *zero_freq_file;
-
- static char pred_set[MAX_SET]; /* flags chars already in first[]*/
-
- static NodePtr free_nodes; /* now use malloc to get at init */
- static NodePtr root; /* the root of the k model trie */
-
- NodePtr create_node() { /* return ptr to a new node or abort */
- char *malloc(); NodePtr nptr;
- if(!gLowMem){
- nptr = (NodePtr) malloc((unsigned) sizeof(Node));
- nptr->next=nptr->up=nil;
- }else nptr=nil;
- if (nptr == nil){ /* SHOULD FORGET AND CONTINUE ON */
- gLowMem = true;
- gReport = false;
- if(gExtraMems) DisposPtr(gExtraMems);
- return(nil);
- }
- next_free++; /* just for show_free_nodes */
- return(nptr); /* if more are needed */
- }
-
- NodePtr move_up(nptr, c) NodePtr nptr; char c; {
-
- NodePtr xptr, fxptr, last_ptr, last_fptr; int state;
-
- /* c &= 0x7F; */
-
- if (nptr == nil) xptr = nil;
- else {
- if (nptr->up == nil) state = START;
- else {
- fxptr = xptr = nptr->up; last_fptr = last_ptr = nil;
- state = SCANNING;
- do {
- if (xptr == nil) state = END;
- else {
- if (fxptr->count > xptr->count) {
- last_fptr = last_ptr; fxptr = xptr; }
- if (xptr->value == c) {
- state = FOUND;
- if (fxptr != xptr) {
- if (last_fptr == nil) nptr->up = xptr;
- else last_fptr->next = xptr;
- last_ptr->next = xptr->next; xptr->next = fxptr;
- } } else { last_ptr = xptr; xptr = xptr->next; }
- } } while (state == SCANNING);
- }
- switch (state) {
- case FOUND:
- if (++(xptr->count) == (*gStartPrefs)->pMaxCount) { /* Forgets on halving 1 */
- /*last_fptr =*/ fxptr = nptr->up;
- while (fxptr != nil) { /* JJD 9-86 */
- fxptr->count++;
- fxptr->count >>= 1;
- fxptr = fxptr->next;
-
- }
- }
- break;
- case START: case END:
- xptr = create_node();
- if (xptr){
- xptr->value = c; xptr->count = 1;
- xptr->up = xptr->next = nil;
- if (state == START) nptr->up = xptr;
- else last_ptr->next = xptr;
- }
- break;
- }
- }
- return (xptr);
- }
-
- find_first(buf) Buffer buf; { /* find 1st char of all pred in context */
-
- int i, order = 0; NodePtr xptr; char *p;
-
- for (p= &pred_set[0]; p< &pred_set[MAX_SET]; p++) *p = '\0';
- for (i=(*gStartPrefs)->pOrder-1; i>=0; i--) {
- if (buf[i] != nil)
- if (buf[i]->up != nil) {
- xptr = buf[i]->up;
- while (xptr != nil) {
- if (!pred_set[(int)xptr->value]) {
- pred_set[(int)xptr->value]++;
- first[order++] = xptr->value;
- }
- xptr = xptr->next;
- } } }
- for (p= &zero_freq[0]; p< &zero_freq[MAX_SET]; p++)
- if (!pred_set[(int)*p]) first[order++] = *p;
- }
-
- char first_pred(buf) Buffer buf; {
- int i = (*gStartPrefs)->pOrder-1;
- for (;;) {
- if (buf[i] != nil)
- if (buf[i]->up != nil) return (buf[i]->up->value);
- if (i == 0) return((char)13);
- i--;
- }
- }
-
- NodePtr scan_up(nptr,c) NodePtr nptr; char c; {
-
- NodePtr xptr;
- if (nptr == nil) return(nil);
- else {
- xptr = nptr->up;
- for (;;) {
- if (xptr == nil) return (nil);
- else if (xptr->value == c) return(xptr);
- else xptr = xptr->next;
- } }
- }
-
- #define MemSize 30720
-
- struct MemBlck{
- char Memory[MemSize+4];
- struct MemBlck *Next;
- } *MemBlock;
-
- int NumMBlocks=0;
-
- char* MemNext=MemSize;
- char *malloc(size)
- {
- size=(size+4)&~4;
- if(MemNext>size){
- MemNext-=size;
- return(char *)&(MemBlock->Memory[MemNext]);
- }else{
- struct MemBlck *new=NewPtrClear(sizeof(struct MemBlck));
- if(new==nil)
- return((char *) 0);
- NumMBlocks++;
- new->Next=MemBlock;
- MemBlock=new;
- MemNext=(char *)MemSize-size;
- return (char *)&(MemBlock->Memory[MemNext]);
- }
- }
-
- SetUpMemory()
- {
- MemBlock=(struct MemBlck *)NewPtrClear(sizeof(struct MemBlck));
- MemBlock->Next=0;
- MemNext=(char *)MemSize;
- NumMBlocks=0;
- }
-
- ClearMemory()
- {
- struct MemBlck *next;
- while(MemBlock){
- next=MemBlock->Next;
- DisposPtr((Ptr)MemBlock);
- MemBlock=next;
- }
- SetUpMemory();
- }
-
- ClearModel()
- {
- int i,c;
- FILE *from;
- char s[255];
-
-
- next_free=0;
- ClearMemory();
-
- CBuf[0] = Buf[0] = root = create_node();
- root->up = root->next = nil;
- root->count = 1;
- for (i=1; i<=(*gStartPrefs)->pOrder; i++) Buf[i] = nil;
- if ((*gStartPrefs)->pHasZero){
- PathNameFromDirID((*gStartPrefs)->pZeroDirID,
- (*gStartPrefs)->pZeroRefNum,s);
- pStrcat(s,(Str255)(*gStartPrefs)->pZeroName);
- if ((from = fopen (p2cstr(s), "r")) != NULL) {
- i = 0;
- while (((int)(c = getc(from))) != EOF) {
- zero_freq[i++] = c;
- if (((int)(c = getc(from))) == EOF) break; /* del NL */
- if (i>127) break; /* test if okay */
- }
- } /* else use built in zero_freq[] */
- }
- }
-
- PrimeFromFile(char *name,Boolean clear)
- {
- int c,i,psize;
- long size,graph;
- FILE *from;
- DialogPtr Therm;
- Rect ThermRect,GrayRect;
- short newRight;
-
- if(clear){
- ClearModel();
- }
- psize = (long) ((*gStartPrefs)->pStartup); /* increase?? */
- if ((from = fopen (name, "r")) != NULL) {
- fseek (from, 0L, 2); /* find out how long it is */
- size = ftell (from);
- if (size > psize) { /* prime max chars at end */
- size=psize;
- fseek (from, -psize, 2); /* start after ^G mark */
- } else rewind (from);
- graph=0;
- if (size==0) size=1;
- Therm=GetNewDialog(rPrimeTherm,(void *) 0,(void *) -1);
- DrawDialog(Therm);
- SetRect(&ThermRect,55,68,227,89);
- SetRect(&GrayRect,56,69,57,88);
- SetPort(Therm);
- FrameRect(&ThermRect);
- while (((int)(c = getc(from))) != EOF) {
- RotateCursor(graph++);
- newRight = ThermRect.left + graph *
- (ThermRect.right - ThermRect.left - 1) / size;
- if (newRight > ThermRect.right) newRight = ThermRect.right;
- if (newRight > GrayRect.right) {
- GrayRect.left = GrayRect.right-1;
- GrayRect.right = newRight;
- EraseRect(&GrayRect);
- FillRect(&GrayRect, qd.gray);
- };
- for (i=(*gStartPrefs)->pOrder; i>0; i--)
- Buf[i] = move_up(Buf[i-1],c);
- }
- fclose (from);
- DisposDialog(Therm);
- Show_Cursor(ARROW_CURSOR);
- }
- }
-
- init_reactive() {
- double atof();
- register int i; FILE *from, *popen();
- char c, *rindex();
- char cbuf[32+1], *cstart, *cend, *end; int full; long size,graph;
- DialogPtr Therm;
- Rect ThermRect,GrayRect;
- short newRight;
- char s[255];
- /* char debug[255];*/
-
-
- /* get a bunch of nodes for starters */
- /* free_nodes = (NodePtr) malloc((unsigned) (max_nodes * sizeof(Node)));
- if (free_nodes == nil) {
- sprintf (tbuf, "cannot allocate %d nodes.\n", max_nodes);
- abortit (tbuf, -1);
- } */
- /* set up root and pointers into model */
- CBuf[0] = Buf[0] = root = create_node();
- root->up = root->next = nil;
- root->count = 1;
- for (i=1; i<=(*gStartPrefs)->pOrder; i++) Buf[i] = nil;
-
- if ((*gStartPrefs)->pHasZero){
- if(PathNameFromDirID((*gStartPrefs)->pZeroDirID,
- (*gStartPrefs)->pZeroRefNum,s) != -1){
- pStrcat(s,(Str255)(*gStartPrefs)->pZeroName);
- if ((from = fopen (p2cstr(s), "r")) != NULL) {
- i = 0;
- while (((int)(c = getc(from))) != EOF) {
- zero_freq[i++] = c;
- if (((int)(c = getc(from))) == EOF) break; /* del NL */
- if (i>127) break; /* test if okay */
- }
- } else /* else give warning and use built in zero_freq[] */
- AlertUser(eCantZero);
- } else /* else give warning and use built in zero_freq[] */
- AlertUser(eCantZero);
- }
-
-
- psize = (long) ((*gStartPrefs)->pStartup);
- if((*gStartPrefs)->pHasPrime){
- if(PathNameFromDirID((*gStartPrefs)->pPrimeDirID,
- (*gStartPrefs)->pPrimeRefNum,s) != -1){
- pStrcat(s,(Str255)(*gStartPrefs)->pPrimeName);
- if ((from = fopen(p2cstr(s),"r")) != NULL) {
-
- fseek (from, 0L, 2); /* find out how long it is */
- size = ftell (from);
- if (size > psize) { /* prime max chars at end */
- size=psize;
- fseek (from, -psize, 2); /* start after ^G mark */
- } else rewind (from);
- graph=0;
- if (size==0) size=1;
- Therm=GetNewDialog(rPrimeTherm,(void *) 0,(void *) -1);
- DrawDialog(Therm);
- /* shouldn't really hardcode this */
- SetRect(&ThermRect,55,68,227,89);
- SetRect(&GrayRect,56,69,57,88);
- SetPort(Therm);
- FrameRect(&ThermRect);
- cstart = cend = cbuf; end = &cbuf[(*gStartPrefs)->pOrder]; full = 0;
- while (((int)(c = getc(from))) != EOF) {
- RotateCursor(graph++);
- newRight = ThermRect.left + graph *
- (ThermRect.right - ThermRect.left - 1) / size;
- if (newRight > ThermRect.right) newRight = ThermRect.right;
- if (newRight > GrayRect.right) {
- GrayRect.left = GrayRect.right-1;
- GrayRect.right = newRight;
- EraseRect(&GrayRect);
- FillRect(&GrayRect, qd.gray);
- };
-
- *cstart = c;
- if (full)
- { ++cstart; if (cstart > end) cstart = cbuf; }
- if (++cend > end)
- { cend = cbuf; full = 1; }
- *cend = '\0';
- for (i=(*gStartPrefs)->pOrder; i>0; i--)
- Buf[i] = move_up(Buf[i-1],c);
- }
- i = 0; /* align the context */
- while (cstart != cend) {
- context[i] = old_context[i] = *cstart++; i++;
- if (cstart > end) cstart = cbuf;
- }
- fclose (from);
- DisposDialog(Therm);
- Show_Cursor(ARROW_CURSOR);
- }else
- AlertUser(eCantPrime);
- }else
- AlertUser(eCantPrime);
- }
- }
-
-